#include "StdAfx.h"
#include "MyMessageHandler.h"
#include "MyActiveInterface.h"
#include "MyPayload.h"
#include "TypeIds.h"
#include "DefaultConfig.h"

namespace CO { namespace MyGroupNamespace { namespace MyPackageNamespace {

MyMessageHandler::MyMessageHandler(SpiFoundation::RoomFW::ActiveObject* parent) :
	//
	// Initialize Base class.
	//
	SpiFoundation::RoomFW::ActiveObject::MessageHandler(parent),
	//
	// TODO:  Initialize attributes.
	//
	myConfigValue_(0)	// TODO:  Initialize attributes to default!
{
}

bool MyMessageHandler::initialize(SpiFoundation::Configuration::ConfigKey& configRoot)
{
	//
	// TODO:  Initialize attributes with configuration settings.  Get key and value
	//		  will create if they don't exist, so be sure everyone uses the same defines
	//		  to avoid spelling errors!
	//
	configRoot.getValue_T(CFG_VAL_PATH_MyIntValue, myConfigValue_);
	return true;
}

bool MyMessageHandler::handle(SpiFoundation::RoomFW::RoomMessageSP message)
{
	//
	// Set return value to true to indicate that message has been handled.
	//
	bool retVal = false;

	//
	// Catch all exceptions here.  Any that escape will result in the thread exiting.
	//
	try
	{
		//
		// TODO:  Handle message for each port.
		//
		switch (message->portIndex().port())
		{
		case MyActiveInterface::PORT_MY_PORT_1:
			{
				//
				// TODO:  Handle each message message for this port.
				//
				switch (message->messageID())
				{
				case MyProtocol::IN_MESSAGE_1:
					{
						//
						// TODO:  Handle the message.
						//
						retVal = true;
						MyPayloadSP msgPayload = ::boost::dynamic_pointer_cast<MyPayload, SpiFoundation::RoomFW::IPayload>(message->releasePayload());
						if (0 != msgPayload)
						{
							this->sendMessage(MyActiveInterface::PORT_MY_PORT_2,
											MyProtocol::IN_MESSAGE_1,
											SpiFoundation::RoomFW::IPayload::ReleaseOwnership(msgPayload));
						}
					}
					break;
				case MyProtocol::IN_MESSAGE_2:
				case MyProtocol::IN_MESSAGE_3:
					{
						//
						// TODO:  Handle the message.
						//
						retVal = true;
						this->sendMessage(MyActiveInterface::PORT_MY_PORT_2, message->messageID());
					}
					break;
				}
			}
			break;
		case MyActiveInterface::PORT_MY_PORT_2:
			{
				//
				// TODO:  Handle each message message for this port.
				//
				switch (message->messageID())
				{
				case MyProtocol::OUT_MESSAGE_1:
					{
						//
						// TODO:  Handle the message.
						//
						retVal = true;
						SpiFoundation::RoomFW::IPayloadSP msgPayload = message->payload()->clone();
						if (0 != msgPayload)
						{
							this->sendMessage(MyActiveInterface::PORT_MY_PORT_1, MyProtocol::OUT_MESSAGE_1, SpiFoundation::RoomFW::IPayload::ReleaseOwnership(msgPayload));
						}
					}
					break;
				case MyProtocol::OUT_MESSAGE_2:
				case MyProtocol::OUT_MESSAGE_3:
					{
						//
						// TODO:  Handle the message.
						//
						retVal = true;
						this->sendMessage(MyActiveInterface::PORT_MY_PORT_1, message->messageID());
					}
					break;
				}
			}
			break;
		default:
			ASSERT(FALSE); // No more ports!
		}
	}
	//
	// TODO: Catch specific errors and handle them correctly.
	//
	DEFAULT_MSG_CATCH_BLOCKS(this, MyActiveInterface::PORT_SYSTEM, message, MyActiveInterface::PORT_MY_PORT_1, MyProtocol::OUT_ERROR)
	return retVal;
}

void MyMessageHandler::onError(SpiFoundation::ErrorHandling::IError& e)
{
	//
	// TODO: Remove logging and Handle the error.  DEFAULT_HSM_CATCH_BLOCKS already logs.
	//
	G_LOG_1_ERROR("MyMessageHandler::onError(%s)", e.what());
}

void MyMessageHandler::onException(std::exception& e)
{
	//
	// TODO: Remove logging and Handle the error.  DEFAULT_HSM_CATCH_BLOCKS already logs.
	//
	G_LOG_1_ERROR("MyMessageHandler::onException(%s)", e.what());
}

void MyMessageHandler::onCatchAllException()
{
	//
	// TODO: Remove logging and Handle the error.  DEFAULT_HSM_CATCH_BLOCKS already logs.
	//
	G_LOG_0_ERROR("MyMessageHandler::onCatchAllException()");
}

}}} // End namespace
